import instance from "@/api/axiosInstance";

export function getWebsocket(sid) {
  if (!sid) return null;

  console.log("尝试连接，sid：" + sid);

  // 从 axios 实例配置中获取 baseURL
  const baseURL = instance.defaults.baseURL;

  // 将 http 转换为 ws，并构建 WebSocket 连接地址
  const wsUrl =
    baseURL.replace("http://", "ws://").replace("https://", "wss://") +
    `/ws/${sid}`;

  console.log("WebSocket连接地址:", wsUrl);

  try {
    return new WebSocket(wsUrl);
  } catch (error) {
    console.error("WebSocket连接创建失败:", error);
    return null;
  }
}
